import { deleteRightTodo, deleteRightItem, spliceGetDataTodo, getSaleNum, getValueArray, setEmptyArray, setArrayValue, setExchangeValue, spliceGetDataItem } from '@/views/sales/common/TableSaleMethods'

export default ({
  state: {
    saleData: '',
    GetsaleData: '',
    imgSrc: '',
    MoresaleForm: {
      name: '',
      date: '',
      type: '1',
      grade: [],
      gender: '1',
      remarks: '',
      main: '',
      second: '',
      moreMoney: '',
      piece: '',
      type1: [{ 'value': '', 'n': '' }],
      type2: [{ 'value': '', 'n': '', isParcel: false }],
      isParcel: false
    }
  },
  mutations: {
    setMoreSaleEmpty (state) {
      state.GetsaleData = ''
      state.saleData = ''
      state.MoresaleForm = {
        name: '',
        date: '',
        type: '1',
        grade: [],
        gender: '1',
        remarks: '',
        main: '',
        second: '',
        moreMoney: '',
        piece: '',
        type1: [{ 'value': '', 'n': '' }],
        type2: [{ 'value': '', 'n': '', isParcel: false }],
        isParcel: false
      }
    },
    onAddDiscountmore (state) { //多买优惠促销增加促销
      if (state.MoresaleForm.type2.length > 2) {
        // alert('可支持三次满减，无法继续添加!')
      } else {
        state.MoresaleForm.type2.push({ 'value': '', 'n': '', isParcel: false })
        state.MoresaleForm.type1.push({ 'value': '', 'n': '' })
      }
    },
    getMoreSaleData (state, type) {
      let getdata = JSON.parse(JSON.stringify(state.saleData))
      // state.GetsaleData = JSON.parse(JSON.stringify(state.saleData));
      getValueArray(getdata, {
        index: type,
        childName: 'productSkuList',
        status: false
      })
      setEmptyArray(getdata, {
        index: type,
        childName: 'productSkuList'
      })
      setArrayValue(getdata, {
        index: type,
        val1: false,
        childName: 'productSkuList'
      })
      if (!state.GetsaleData.length) { //首次为空
        state.GetsaleData = getdata
      } else { // 防止每次更新的淹没其他的
        //把没有的第一层数据存在，GetsaleData里面
        //把没有的第二层的数据存在, GetsaleData里面
        spliceGetDataItem(getdata, state.GetsaleData)
        spliceGetDataTodo(getdata, state.GetsaleData)
      }
      if (type !== 'buttonChecked') {
        setExchangeValue(state.saleData, { index1: 'checked', index: 'buttonChecked', childName: 'productSkuList' })
      }
    },
    onReduceDiscountmore (state, index) { //多买优惠促销减去促销
      state.MoresaleForm.type2.splice(index, 1)
      state.MoresaleForm.type1.splice(index, 1)
    },
    deleteMoreSale (state, object) {
      if (object.todo && !object.todo.buttonChecked || object.item && !object.item.buttonChecked) {
        if (object.todo) { //没子集
          deleteRightTodo(object.item, object.todo, state.GetsaleData)
        } else { //有子集
          deleteRightItem(object.item, state.GetsaleData)
        }
      }
    }
  },
  actions: {
  },
  getters: {
    getMoreSaleNum (state) {
      return getSaleNum(state.GetsaleData, { childName: 'productSkuList' })
    },
    moreSaleGrade (state, getters) { //多买优惠促销活动对象
      let newarray = []
      for (let item of state.MoresaleForm.grade) {
        for (let todo of getters.storeTypeData) {
          if (item == todo.value) {
            newarray.push(todo.text)
          }
        }
      }
      return newarray.join(',')
    },
    moreSaleGender (state) { //多买优惠促销参与方式
      let word
      switch (state.MoresaleForm.gender) {
        case '1' :
          word = '全场商品参加'
          break
        case '2' :
          word = '部分商品参加'
          break
        case '3' :
          word = '部分商品不参加'
          break
      }
      return word
    },
    moreSaleType (state) { //多买优惠促销类型
      let word
      if (state.MoresaleForm.type == '1') {
        word = '满M件减N件'
      } else if (state.MoresaleForm.type == '2') {
        word = 'M件N折'
      } else if (state.MoresaleForm.type == '3') {
        word = 'M元任选N件'
      }
      return word
    }
  }
})
